Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove injected props in Admin and Layout components #9591

Merged
merged 10 commits into from
Jan 24, 2024

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Jan 17, 2024

Problems

In order to build a custom layout for <Admin layout>, developers must clone props:

// in src/MyLayout.js
import { Layout } from 'react-admin';
import { MyAppBar } from './MyAppBar';

export const MyLayout = props => (
   <Layout {...props} appBar={MyAppBar} />
);

Developers wonder: what is props? What's its type? Why do I have to pass it down? It’s not so usual in modern React.

The same happens for other components that can be set on <Layout>: appBar, menu, error, catchAll.

Solution

The only useful data injected is title and dashboard. The rest is useless as the data can be obtained in another way.

Use a context instead of props injection for these values. That way, a custom layout just needs to pass the children down (which requires no explaining).

// in src/MyLayout.js
import { Layout } from 'react-admin';
import { MyAppBar } from './MyAppBar';

export const MyLayout = ({ children }) => (
  <Layout appBar={MyAppBar}>
     {children}
  </Layout>
);

Also, remove deprecated prop injection as there are alternatives.

Tasks

  • Remove prop injection in layout
  • Remove prop injection in menu
  • Remove prop injection in appBar
  • Remove prop injection in error
  • Remove prop injection in catchAll
  • Change types to reflect that
  • Update documentation and jsDoc
  • Fix outdated props injection in jsDoc snippets
  • Write Upgrade guide

## Problem

The Layout component is the last one requiring props cloning in user land:

```jsx
// in src/MyLayout.js
import { Layout } from 'react-admin';
import { MyAppBar } from './MyAppBar';

export const MyLayout = props => (
   <Layout {...props} appBar={MyAppBar} />
);
```

Developers wonder: what is props? Why do I have to pass it down? It’s not so usual in modern React.

## Solution

Use a context instead, and just pass the children down (which requires no explaining).

```jsx
// in src/MyLayout.js
import { Layout } from 'react-admin';
import { MyAppBar } from './MyAppBar';

export const MyLayout = ({ children }) => (
  <Layout appBar={MyAppBar}>
     {children}
  </Layout>
);
```
@fzaninotto fzaninotto added RFR Ready For Review and removed WIP Work In Progress labels Jan 22, 2024
@fzaninotto fzaninotto changed the title Update Layout to require only children prop Remove injected props in Admin and Layout components Jan 22, 2024
@djhi djhi self-requested a review January 23, 2024 13:56
@adguernier adguernier self-requested a review January 23, 2024 14:00
Copy link
Contributor

@djhi djhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

docs/Admin.md Outdated Show resolved Hide resolved
docs/Upgrade.md Outdated Show resolved Hide resolved
Co-authored-by: Gildas Garcia <1122076+djhi@users.noreply.github.com>
@adguernier adguernier merged commit 76701a6 into next Jan 24, 2024
11 checks passed
@adguernier adguernier deleted the layout-no-props-cloning branch January 24, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants